package com.kha.listone.screen.single;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.StringItem;

import com.kha.listone.ListOne;
import com.kha.listone.common.SingleScreen;
import com.kha.listone.common.model.TaskModel;
import com.kha.listone.util.Commands;
import com.kha.listone.util.DateFormatter;
import com.kha.listone.util.Globals;
import com.kha.listone.util.ImageLoader;
import com.kha.listone.util.operation.Operation;

public class DetailScreen extends SingleScreen implements ItemCommandListener {

	private StringItem taskName;
	private StringItem taskDueDate;
	private StringItem taskDeadlineDate;
	private ImageItem taskRecurrenceType;
	private StringItem taskRecurrenceDate;
	private StringItem taskNote;
	private StringItem taskTags;
	private ImageItem taskImportant;
	private ImageItem taskStatus;

	private TaskModel model;

	public DetailScreen(ListOne midlet, String title, TaskModel model) {
		super(midlet, title);

		this.model = model;

		enququeOperation.enqueue(new RenderTask(this));
		enququeOperation.start();
	}

	protected void initScreen() {

		taskName = new StringItem("Name", "");
		taskDueDate = new StringItem("Start", "");
		taskDeadlineDate = new StringItem("Deadline", "");
		taskRecurrenceType = new ImageItem("",
				ImageLoader.load(ImageLoader.CATEGORY_CANVAS),
				ImageItem.LAYOUT_CENTER, "Caption");
		taskRecurrenceDate = new StringItem("Repeat day", "");
		taskNote = new StringItem("Note", "");
		taskTags = new StringItem("Tags", "");
		taskImportant = new ImageItem("",
				ImageLoader.load(ImageLoader.CATEGORY_CANVAS),
				ImageItem.LAYOUT_CENTER, "Caption");
		taskStatus = new ImageItem("",
				ImageLoader.load(ImageLoader.CATEGORY_CANVAS),
				ImageItem.LAYOUT_CENTER, "Caption");		
		
		taskRecurrenceType.setDefaultCommand(Commands.BACK);
		
		taskRecurrenceType.setItemCommandListener(this);

		super.initScreen();
	}

	public void commandAction(Command c, Displayable d) {

		super.commandAction(c, d);
	}

	public void refresh(TaskModel model) {
		this.model = model;
		enququeOperation.enqueue(new RenderTask(this));
		enququeOperation.restart();
		super.refresh();
	}

	public TaskModel getModel() {
		return model;
	}

	public void setModel(TaskModel model) {
		this.model = model;
	}

	private class RenderTask implements Operation {

		private DetailScreen screen;

		public RenderTask(DetailScreen screen) {
			this.screen = screen;
		}

		public void execute() {
			synchronized (screen) {
				taskName.setText(model.getTaskName());
				taskDueDate.setText(DateFormatter.format(model.getDueDate(),
						Globals.settingListOne.getDateFormat()));

				taskDeadlineDate.setText(DateFormatter.format(
						model.getDeadlineDate(),
						Globals.settingListOne.getDateFormat()));

				if (model.getRecurrenceStatus() == TaskModel.RECURRENCE_ONCE) {
					taskRecurrenceType.setImage(ImageLoader
							.load(ImageLoader.CATEGORY_1));
				} else if (model.getRecurrenceStatus() == TaskModel.RECURRENCE_DAILY) {
					taskRecurrenceType.setImage(ImageLoader
							.load(ImageLoader.CATEGORY_2));
				} else {
					taskRecurrenceType.setImage(ImageLoader
							.load(ImageLoader.CATEGORY_CANVAS));
				}

				taskRecurrenceDate = new StringItem("Repeat day", "");
				taskNote = new StringItem("Note", "");
				taskTags = new StringItem("Tags", "");
				taskImportant = new ImageItem("",
						ImageLoader.load(ImageLoader.CATEGORY_CANVAS),
						ImageItem.LAYOUT_RIGHT, "Caption");
				taskStatus = new ImageItem("",
						ImageLoader.load(ImageLoader.CATEGORY_CANVAS),
						ImageItem.LAYOUT_RIGHT, "Caption");

				screen.append(taskRecurrenceType);
				screen.append(taskImportant);
				screen.append(taskStatus);

				screen.append(taskName);
				screen.append(taskDueDate);
				screen.append(taskDeadlineDate);

				screen.append(taskRecurrenceDate);
				screen.append(taskNote);
				screen.append(taskTags);
				
			}
		}
	}

	public void commandAction(Command c, Item item) {
		if (c == Commands.BACK){
			mainMIDlet.back();
		}
		
	}

}
